{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "## Using HuggingFace Text Generation Pipeline"
      ],
      "metadata": {
        "id": "8WYE_29w3bPj"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "luOwrS6kBGX0",
        "outputId": "cb880954-527a-4edc-ee5c-d64ec843a4c5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[{'generated_text': [{'role': 'user',\n",
              "    'content': 'What is the capital of France?'},\n",
              "   {'role': 'assistant', 'content': 'The capital of France is Paris.'}]}]"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ],
      "source": [
        "# Use a pipeline as a high-level helper\n",
        "from transformers import pipeline\n",
        "\n",
        "messages = [\n",
        "    {\"role\": \"user\", \"content\": \"What is the capital of France?\"},\n",
        "]\n",
        "pipe = pipeline(\"text-generation\", model=\"TinyLlama/TinyLlama-1.1B-Chat-v1.0\")\n",
        "pipe(messages)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Using HuggingFace Inference API"
      ],
      "metadata": {
        "id": "mfB5jwDu3g_J"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from huggingface_hub import InferenceClient\n",
        "\n",
        "client = InferenceClient(api_key=\"api_key\")\n",
        "\n",
        "for message in client.chat_completion(\n",
        "\tmodel=\"TinyLlama/TinyLlama-1.1B-Chat-v1.0\",\n",
        "\tmessages=[{\"role\": \"user\", \"content\": \"What is the capital of France?\"}],\n",
        "\tmax_tokens=500,\n",
        "\tstream=True,\n",
        "):\n",
        "    print(message.choices[0].delta.content, end=\"\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g_ztA7FRCWpJ",
        "outputId": "7f3eac7e-6b64-428f-b868-08e76fb6bcd9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The capital of France (le chef-lieu de France) is Paris. Paris is the seat of the Parliament of France, the National Assembly, the Conseil d'Etat, the French Council of State, the Constitutional Council, and some public offices."
          ]
        }
      ]
    }
  ]
}